package pl.us.ij.plugins.segmentation.forms;

import com.intellij.uiDesigner.core.Spacer;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import ij.ImagePlus;
import pl.us.ij.plugins.segmentation.SwitchImageListener;
import pl.us.ij.plugins.segmentation.commands.threshold.OneLevelThresholdCommand;
import pl.us.ij.plugins.segmentation.commands.threshold.OneLevelThresholdCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-26
 * Time: 00:17:07
 * To change this template use File | Settings | File Templates.
 */
public class ThresholdsOneLevelForm extends CategoryInfo implements SwitchImageListener {
    private JPanel semiThresholdPanel;
    private JSpinner semiThresholdSpinner;
    private JButton semiThresholdButton;
    private JLabel minMaxLabel;

    public ThresholdsOneLevelForm(IActionHandler actionHandler) {
        semiThresholdSpinner.setModel(new SpinnerNumberModel(127, 1, 255, 1));

        actionHandler.setAction(semiThresholdButton, OneLevelThresholdCommand.class, getSemiThresholdParams());

        minMaxLabel.setText("");
    }

    @Override
    public String getTitle() {
        return Localization.getString("thresholdsSemiForm.title");
    }

    @Override
    public JPanel getPanel() {
        return semiThresholdPanel;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private ICommandParamGather getSemiThresholdParams() {
        return new ICommandParamGather() {
            public CommandParams getCommandParams() {
                OneLevelThresholdCommandParams params = new OneLevelThresholdCommandParams();
                params.setThreshold(((Number) semiThresholdSpinner.getValue()).intValue());
                return params;
            }
        };
    }

    @Override
    public void imageActivated(ImagePlus imp) {
        ImagePlusInfo impInfo = new ImagePlusInfo(imp);

        int mx = (int) impInfo.getMax();
        int mn = (int) impInfo.getMin();

        Number value = (int) impInfo.fitRange(((Number) semiThresholdSpinner.getValue()).floatValue());

        semiThresholdSpinner.setModel(new SpinnerNumberModel(value, mn, mx, 1));

        minMaxLabel.setText("Min: " + mn + " Max: " + mx);
    }

    @Override
    public void imageDeactivated(ImagePlus deactivatedImp, ImagePlus imp) {
        if (imp == null) {
            minMaxLabel.setText("");
        }
        else {
            imageActivated(imp);
        }
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:noGrow", "center:d:noGrow"));
        semiThresholdPanel = new JPanel();
        semiThresholdPanel.setLayout(new FormLayout("fill:d:noGrow,left:4dlu:noGrow,fill:48px:noGrow,left:4dlu:noGrow,fill:max(d;124px):noGrow", "center:d:noGrow,top:4dlu:noGrow,center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(semiThresholdPanel, cc.xy(1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("thresholdsSemiForm.thresholdLabel"));
        semiThresholdPanel.add(label1, cc.xy(1, 1));
        semiThresholdSpinner = new JSpinner();
        semiThresholdPanel.add(semiThresholdSpinner, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        semiThresholdButton = new JButton();
        this.$$$loadButtonText$$$(semiThresholdButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("thresholdsSemiForm.applyLabel"));
        semiThresholdPanel.add(semiThresholdButton, cc.xyw(1, 5, 3));
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        semiThresholdPanel.add(spacer1, cc.xyw(1, 3, 3, CellConstraints.DEFAULT, CellConstraints.FILL));
        minMaxLabel = new JLabel();
        minMaxLabel.setText("");
        semiThresholdPanel.add(minMaxLabel, cc.xy(5, 1));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }
}
